package services;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import repositories.AdministradorRepository;
import security.Authority;
import security.LoginService;
import security.UserAccount;
import domain.Administrador;
import forms.AdministradorForm;

@Service
@Transactional
public class AdministradorService {

	@Autowired
	private AdministradorRepository administradorRepository;
	
	// Supporting Services
	
	@Autowired
	private VecinoService vecinoService;

	public AdministradorService() {
		super();
	}

	// Simple CRUD methods
	public Administrador create() {
		Administrador result;
		UserAccount userAccount;
		Authority authority;

		authority = new Authority();

		userAccount = new UserAccount();
		authority.setAuthority("ADMINISTRADOR");
		userAccount.addAuthority(authority);

		result = new Administrador();
		result.setUserAccount(userAccount);

		return result;
	}

	public void save(Administrador administrador) {
		Assert.isTrue(findByPrincipal() instanceof Administrador); 
		Assert.notNull(administrador);
		if(administrador.getId()==0){
			Assert.isNull(findByUsername(administrador.getUserAccount().getUsername()));
		}
		Assert.isNull(vecinoService.findByUsername(administrador.getUserAccount().getUsername()));
		administradorRepository.save(administrador);
	}

	public Administrador findOne(int administratorId){
		Administrador result;
		
		result = administradorRepository.findOne(administratorId);
		
		return result;
	}
	
	// Other Business Methods
	public Administrador findByPrincipal() {
		Administrador result;
		UserAccount userAccount;

		userAccount = LoginService.getPrincipal();
		Assert.notNull(userAccount);

		result = findByUserAccount(userAccount);
		Assert.notNull(result);

		return result;
	}

	public Administrador findByUserAccount(UserAccount userAccount) {
		Assert.notNull(userAccount);
		Administrador result;

		result = administradorRepository.findByUserAccountId(userAccount.getId());

		return result;
	}
	
	public Administrador findByUsername (String username){
		Assert.notNull(username);
		Administrador result;
		result = administradorRepository.findByUsername(username);
		return result;
	}

	// Reconstruction method
	public Administrador reconstruct(AdministradorForm administradorForm) {
		Assert.notNull(administradorForm);
		Administrador result;
		Md5PasswordEncoder encoder = new Md5PasswordEncoder();
		String password = administradorForm.getPassword();
		String hash = encoder.encodePassword(password, null);

		result = create();
		result.getUserAccount().setUsername(administradorForm.getUsername());
		result.getUserAccount().setPassword(hash);
		result.setNombre(administradorForm.getName());
		result.setApellidos(administradorForm.getSurname());
		result.setEmail(administradorForm.getEmail());

		return result;
	}
}
